import axios from 'axios'

export function request(config) {
  const instance = axios.create({
    baseURL: 'https://devapi.qweather.com/v7/weather',
    timeout: 5000
  })

  instance.interceptors.request.use((config) => {
    config.params.key = '3c857080d7264b01bc62620947823430'
    return config
  }),
    (err) => {
      console.log(err)
    }
  return instance(config)
}

export function cityLookup(config) {
  const instance = axios.create({
    baseURL: 'https://geoapi.qweather.com/v2/city/lookup?',
    timeout: 5000
  })

  instance.interceptors.request.use((config) => {
    config.params.range = 'cn'
    config.params.key = '3c857080d7264b01bc62620947823430'
    return config
  }),
    (err) => {
      console.log(err)
    }
  return instance(config)
}

export function autoGetIP() {
  const instance = axios.create({
    baseURL:
      'https://restapi.amap.com/v3/ip?key=251f3b47936bf2e44213d0b3a4172628',
    timeout: 5000
  })

  return instance()
}

// 实时天气
export function getNowData(pos) {
  return request({
    url: '/now',
    params: {
      location: pos
    }
  })
}

// 逐小时天气预报
export function getFutureHoursData(pos) {
  return request({
    url: '/24h',
    params: {
      location: pos
    }
  })
}

// 未来7天天气
export function get7DaysData(pos) {
  return request({
    url: '/7d',
    params: {
      location: pos
    }
  })
}

// 城市信息搜索
export function getCitylist(pos) {
  return cityLookup({
    params: {
      location: pos
    }
  })
}
